﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbcCorp.DTO;
using DAO.EntityModel;

namespace AbcCorp.DAO
{
    /// <summary>
    /// Lớp sản phẩm
    /// </summary>
    public class ProductDao
    {
        #region Variable

        /// <summary>
        /// Khai báo tượng entity model
        /// </summary>
        private readonly ABCCorpEntities _abcCorpEntities = new ABCCorpEntities();

        #endregion

        #region Methods

        /// <summary>
        /// Thêm sản phẩm
        /// </summary>
        /// <param name="newProduct">Sản phẩm mới muốn thêm</param>
        public bool AddProduct(ProductDto newProduct)
        {
            var product = new Product();
            CopyObject<ProductDto, Product>.CopyProperties(newProduct, product);
            _abcCorpEntities.Products.AddObject(product);
            return _abcCorpEntities.SaveChanges() > 0;
        }

        /// <summary>
        /// Cập nhập thông tin sản phẩm
        /// </summary>
        /// <param name="newInfo">Thông tin mới</param>
        /// <param name="id">Mã sản phẩm</param>
        public bool UpdateProduct(ProductDto newInfo, int id)
        {
            var prodinfo = from prd in _abcCorpEntities.Products.Where(p => p.ProductID.Equals(id))
                           select prd;
            var product = prodinfo.SingleOrDefault();

            if (product == null) return false;

            newInfo.ProductID = id;
            CopyObject<ProductDto, Product>.CopyProperties(newInfo, product);
            return _abcCorpEntities.SaveChanges() > 0;
        }

        /// <summary>
        /// Xóa sản phẩm
        /// </summary>
        /// <param name="prodid">Mã sản phẩm</param>
        public bool DeleteProduct(int prodid)
        {
            var prodinfo = from prd in _abcCorpEntities.Products.Where(p => p.ProductID.Equals(prodid))
                           select prd;
            var product = prodinfo.SingleOrDefault();
            if (product == null) return false;
            _abcCorpEntities.Products.DeleteObject(product);
            return _abcCorpEntities.SaveChanges() > 0;
        }

        /// <summary>
        /// Lấy ra danh sách tất cả các sản phẩm
        /// </summary>
        /// <returns>List ProductDto</returns>
        public IList<ProductDto> GetProducts()
        {
            IList<ProductDto> lstProduct = new List<ProductDto>();
            var prodinfo = from prd in _abcCorpEntities.Products
                           select prd;
            if (prodinfo.FirstOrDefault() == null) return null;
            foreach (var product in prodinfo)
            {
                var productDto = new ProductDto();
                CopyObject<Product, ProductDto>.CopyProperties(product, productDto);
                lstProduct.Add(productDto);
            }
            return lstProduct;
        }

        /// <summary>
        /// Lấy thông tin sản phẩm bằng mã sản phẩm
        /// </summary>
        /// <param name="productid">Mã sản phẩm</param>
        /// <returns>ProductDto</returns>
        public ProductDto GetProduct(int productid)
        {
            var prodinfo = from prd in _abcCorpEntities.Products.Where(p => p.ProductID.Equals(productid))
                           select prd;
            var product = prodinfo.SingleOrDefault();

            if (product == null) return null;
                
            var productDto = new ProductDto();
            CopyObject<Product, ProductDto>.CopyProperties(product, productDto);
            return productDto;
        }

        /// <summary>
        /// Lấy sản phẩm bằng mã danh mục
        /// </summary>
        /// <param name="catid">Mã danh mục</param>
        public IList<ProductDto> GetProductByCategory(int catid)
        {
            IList<ProductDto> lstProduct = new List<ProductDto>();
            var prodinfo = from prd in _abcCorpEntities.Products.Where(p => p.CategoryID.Equals(catid))
                           select prd;
            if (prodinfo.FirstOrDefault() == null) return null;
            foreach (var product in prodinfo)
            {
                var productDto = new ProductDto();
                CopyObject<Product, ProductDto>.CopyProperties(product, productDto);
                lstProduct.Add(productDto);
            }
            return lstProduct;
        }

        /// <summary>
        /// Tim kiem san pham
        /// </summary>
        /// <param name="keySearch">Tu khoa tim kiem</param>
        /// <param name="catid">Danh muc tim kiem</param>
        /// <returns>Danh sach san pham</returns>
        public IList<ProductDto> FindProduct(string keySearch, int catid)
        {
            IList<ProductDto> lstProduct = new List<ProductDto>();
            var products =
                from prod in
                    _abcCorpEntities.Products.Where(
                        p => p.ProductName.Contains(keySearch) && p.CategoryID.Equals(catid))
                select prod;

            if (products.FirstOrDefault() == null) return null;

            foreach (var product in products)
            {
                var productDto = new ProductDto();
                CopyObject<Product, ProductDto>.CopyProperties(product, productDto);
                lstProduct.Add(productDto);
            }
            return lstProduct;
        }
        #endregion
    }
}
